package models.user;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;

/**
 * 用户联系方式表
 * 
 * @author zhangpeng
 * 
 */

@Entity
@Table(name = "link_user_info")
public class LinkUserInfo extends Model {

	private static final long serialVersionUID = 1L;

	@Id
	public long id;
	public String username;
	public String tel;
	public String mobile;
	/**
	 * 移动电话验证状态
	 */
	public boolean mobile_status = false;
	/**
	 * 支付宝
	 */
	public String alipay;
	public String qq;
	public String msn;
	/**
	 * 阿里旺旺
	 */
	public String wangwang;
	public String email;
	/**
	 * 电子邮箱验证状态
	 */
	public boolean email_status = false;

	public static Model.Finder<Long, LinkUserInfo> find = new Model.Finder<Long, LinkUserInfo>(Long.class, LinkUserInfo.class);

	public static LinkUserInfo getInstances() {
		return new LinkUserInfo();
	}

	/**
	 * 创建用户联系方式
	 * 
	 * @param lui
	 * @return
	 * @throws MengException
	 */
	public boolean createLUI(LinkUserInfo lui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(lui.username)) {
			throw new MengException("300001", "用户名不存在");
		}

		if (verifyLinkUserInfo(lui.username)) {
			throw new MengException("300002", "用户联系方式已存在");
		}

		try {
			lui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createLUI Exception," + t.getMessage());
			throw new MengException("300003", "用户联系方式保存失败");
		}
	}

	public boolean createLUI(UserInfo ui) throws MengException {

		if (verifyLinkUserInfo(ui.username)) {
			throw new MengException("300008", "用户基本信息已存在");
		}
		LinkUserInfo lui = getInstances();
		lui.username = ui.username;
		lui.email = ui.email;
		try {
			lui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createBUI Exception," + t.getMessage());
			throw new MengException("300009", "用户基本信息保存失败");
		}
	}

	/**
	 * 用户联系方式修改
	 * 
	 * @param lui
	 * @return
	 * @throws MengException
	 */
	public boolean modifyLUI(LinkUserInfo lui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(lui.username)) {
			throw new MengException("300004", "用户名不存在");
		}
		LinkUserInfo l = getLUI(lui.username);
		lui.id = l.id;
		lui.mobile_status = l.mobile_status;
		lui.email_status = l.email_status;
		try {
			lui.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyLUI Exception," + t.getMessage());
			throw new MengException("300005", "用户联系方式修改失败");
		}
	}

	/**
	 * 获取用户联系方式
	 * 
	 * @param username
	 * @return
	 * @throws MengException
	 */
	public LinkUserInfo getLUI(String username) throws MengException {

		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("300006", "用户名不存在");
		}

		try {

			return find.where().eq("username", username).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getLUI Exception," + t.getMessage());
			throw new MengException("300007", "获取用户联系方式失败");
		}
	}

	public boolean activeEmail(String username, String email) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("300010", "用户名不存在");
		}
		
		try {
			LinkUserInfo lui = getLUI(username);
			if(email.equals(lui.email)){
				lui.email_status  = true;
				lui.update();
				return true;
			}
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo activeEmail Exception," + t.getMessage());
			throw new MengException("300011", "更新email状态失败");
		}
		
		return false;

	}

	public boolean activeMobile(String username, String mobile) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("300012", "用户名不存在");
		}
		
		try {
			LinkUserInfo lui = getLUI(username);
			if(mobile.equals(lui.mobile)){
				lui.mobile_status = true;
				lui.update();
				return true;
			}
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo activeMobile Exception," + t.getMessage());
			throw new MengException("300013", "更新手机状态失败");
		}
		
		return false;
	}

	public boolean verifyLinkUserInfo(String username) throws MengException {

		if (getLUI(username) != null) {
			return true;
		}

		return false;
	}

}
